// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CameraCalibrationDataLoaderTest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Vision
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows.Media.Imaging;

    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Vision.Runtime.GroundTruthExtraction;
    using Microsoft.VisualStudio.TestTools.UnitTesting;    
    
    /// <summary>
    /// Camera Calibration Data Loader related tests.
    /// </summary>
    [TestClass]
    public class CameraCalibrationDataLoaderTest
    {
        /// <summary>
        /// Test Color Intrinsic Params.
        /// </summary>
        private static readonly double[] TestColorIntrinsicParams = { 989.704, 0, 988.807, 657.014, 344.698 };

        /// <summary>
        /// Test Color R Distortion Params.
        /// </summary>
        private static readonly double[] TestColorRDistortionParams = { 0.0267711, -0.0654381, -0.000869768, -0.00127955, 0 };

        /// <summary>
        /// Test Landmark Points
        /// </summary>
        private static readonly CameraCalibrationInfo TestCameraCalibrationInfo = new CameraCalibrationInfo()
        {
            ColorIntrinsicParams = TestColorIntrinsicParams,
            ColorRDistortionParams = TestColorRDistortionParams
        };

        /// <summary>
        /// Tests Camera Calibration data loader export/import.
        /// </summary>
        [Priority(0)]
        [TestMethod]
        [TestCategory("Unit")]
        public void CameraCalibrationInfoSaveAndLoadTest()
        {
            string testFileName = "WebcamCalibration.xml";

            DataLoader<CameraCalibrationInfo>.ExportData(testFileName, TestCameraCalibrationInfo);
            CameraCalibrationInfo readCameraCalibrationInfo = DataLoader<CameraCalibrationInfo>.ImportData(testFileName);

            Assert.IsNotNull(readCameraCalibrationInfo);

            Assert.AreEqual(TestCameraCalibrationInfo.ColorIntrinsicParams.Length, readCameraCalibrationInfo.ColorIntrinsicParams.Length);
            Assert.AreEqual(TestCameraCalibrationInfo.ColorRDistortionParams.Length, readCameraCalibrationInfo.ColorRDistortionParams.Length);

            for (int i = 0; i < TestCameraCalibrationInfo.ColorIntrinsicParams.Length; ++i)
            {
                Assert.AreEqual(TestCameraCalibrationInfo.ColorIntrinsicParams[i], readCameraCalibrationInfo.ColorIntrinsicParams[i]);
            }

            for (int i = 0; i < TestCameraCalibrationInfo.ColorRDistortionParams.Length; ++i)
            {
                Assert.AreEqual(TestCameraCalibrationInfo.ColorRDistortionParams[i], readCameraCalibrationInfo.ColorRDistortionParams[i]);
            }
        }
    }
}
